/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

/**
 *
 * @author Green
 */
public class Banker implements IMotionFinish {

    private Deck deck;
    public Point position;
    public ArrayList<GUICard> lstCardsOnTable;
    private ArrayList<Point> lstPlayedCardPosition;
    int nCardOnTheWay = 0;
    private GamePanel gp;
    

    public Banker(GamePanel gp) {
        this.deck = new Deck();

        this.gp = gp;
        this.position = new Point();
        this.position.x = gp.getWidth() / 2 - GUICard.width / 2;
        this.position.y = gp.getHeight() / 2 - GUICard.height / 2;

        this.lstCardsOnTable = new ArrayList<GUICard>(4);
        this.lstPlayedCardPosition = new ArrayList<Point>(4);
        this.initCardPosition();

    }

    private void initCardPosition() {
        Point begPoint = new Point(gp.getWidth() / 2 - GUICard.width / 2, gp.getHeight() / 2 - GUICard.height);
        Point p = new Point();
        p.x = begPoint.x;
        p.y = begPoint.y + GUICard.height - 10;
        this.lstPlayedCardPosition.add(p);

        p = new Point();
        p.x = begPoint.x - GUICard.width + 10;
        p.y = begPoint.y + GUICard.height / 2;
        this.lstPlayedCardPosition.add(p);

        p = new Point();
        p.x = begPoint.x;
        p.y = begPoint.y + 10;
        this.lstPlayedCardPosition.add(p);

        p = new Point();
        p.x = begPoint.x + GUICard.width - 10;
        p.y = begPoint.y + GUICard.height / 2;
        this.lstPlayedCardPosition.add(p);
    }

    public void dealCard(int rank, int suit, Gambler gambler) {
        GUICard card = this.deck.getCard(rank, suit);
        Hearts.motionManager.addMotion_DealCard(card, gambler);
    }

    public void sendCardToLostGambler(int rank, int suit, Gambler gambler) {
        GUICard card = this.getCardOnTable(rank, suit);
        assert card == null : "whyyyyyyyyyyy";
        if (card == null) {
            return;
        }
        Hearts.motionManager.addMotion_TakenCard(card, gambler);
        this.nCardOnTheWay--;
    }

    private GUICard getCardOnTable(int rank, int suit) {
        for (int i = 0; i < this.lstCardsOnTable.size(); i++) {
            GUICard card = this.lstCardsOnTable.get(i);
            if (card.rank == rank && card.suit == suit) {
                return card;
            }
        }

        return null;
    }

    public GUICard getFirstCardOnBoard() {
        if(this.lstCardsOnTable.size() == 0)
            return null;

        return this.lstCardsOnTable.get(0);
    }

    public boolean isCardAvailable(int rank, int suit) {
        GUICard card = this.deck.getCard(rank, suit);
        return card == null;
    }

    //Kiểm tra card này đánh ra có hợp lệ ko-cái này là demo thôi,cần phải có GameRule
    //mới chính xác
    public boolean isAcceptableCard( GUICard card) {
        if(this.lstCardsOnTable.size() == 0) {
            return true;
        }

        GUICard first = this.lstCardsOnTable.get(0);
        if(card.isSameSuit(first))
            return true;

        return false;
    }

    public Point nextPosition(int id) {
//        int i = this.nCardOnTheWay;
//        this.nCardOnTheWay++;
        Point p;
//        if (i > 0) {
//            i--;
//        }

        p = this.lstPlayedCardPosition.get(id);

        return p;
    }

    public int countAvailableCard() {
        return this.deck.lstCards.size();
    }

    public void drawOnBoardCards(Graphics g) {
        for (int i = 0; i < this.lstCardsOnTable.size(); i++) {
            GUICard card = this.lstCardsOnTable.get(i);
            card.draw(g);
        }
    }

    public void drawDecksBackFace(Graphics g) {
        if (!this.deck.lstCards.isEmpty()) {
            this.deck.drawBackFace(g, this.position.x, this.position.y);
        }
    }

    public void reset() {
        this.lstCardsOnTable.clear();
        this.nCardOnTheWay = 0;
        this.deck.reset();
        for(int i = 0;i < 52;i++) {
            GUICard card = this.deck.lstCards.get(i);
            card.position = (Point)this.position.clone();
        }
    }

    public void end(Motion motion) {
        GUICard card = motion.getCard();
        this.lstCardsOnTable.add(card);
    }
}
